﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lib.Web.Mvc;
using Northwind.Repositories;
using Northwind.Repositories.Entity;

namespace jQuery.UI.Autocomplete.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        #region Fields
        private ITerritoriesRepository _territoriesRepository;
        #endregion

        #region Constructors
        public HomeController()
            : this(new TerritoriesRepository())
        {
        }

        public HomeController(ITerritoriesRepository territoriesRepository)
        {
            _territoriesRepository = territoriesRepository;
        }
        #endregion

        #region Actions
        public ViewResult Autocomplete()
        {
            return View();
        }

        [NoCache]
        public ActionResult AutocompleteAsync(string term)
        {
            return Json((from territory in _territoriesRepository.FindStartingWith(term) select territory.Description).ToArray(), JsonRequestBehavior.AllowGet);
        }
        #endregion
    }
}
